<?php

namespace zhtfrontend\models\datas;

use common\components\extenders\MenuManage;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%mechanic_subsidy}}".
 *
 * @property integer $id
 * @property integer $mechanic_id
 * @property integer $class_id
 * @property integer $lesson
 * @property integer $status
 * @property integer $class_date
 * @property integer $people_number
 * @property string $amount
 * @property string $lesson_amount
 * @property integer $type
 */
class MechanicSubsidy extends \zhtfrontend\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%mechanic_subsidy}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['mechanic_id', 'class_id', 'lesson', 'status', 'class_date', 'people_number', 'type','created_at','settlement_time'], 'integer'],
            [['amount', 'lesson_amount'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增id'),
            'mechanic_id' => Yii::t('zhtfrontend', '机构id'),
            'class_id' => Yii::t('zhtfrontend', '班级id'),
            'lesson' => Yii::t('zhtfrontend', '课次'),
            'status' => Yii::t('zhtfrontend', '0 未结算 1 已结算'),
            'class_date' => Yii::t('zhtfrontend', '上课时间'),
            'people_number' => Yii::t('zhtfrontend', '学生数量'),
            'amount' => Yii::t('zhtfrontend', '补贴金额/分成金额'),
            'lesson_amount' => Yii::t('zhtfrontend', '单课次金额/分成金额'),
            'type' => Yii::t('zhtfrontend', '1、按学生 2、按课程'),
            'settlement_time' => Yii::t('zhtfrontend', '结算时间'),
            'created_at' => Yii::t('zhtfrontend', '创建时间'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();

        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['settlement_time'] = function () {
            if($this->settlement_time > 0)
                return \Yii::$app->formatter->asDatetime($this->settlement_time);
            else
                return \Yii::t('zhtfrontend', '--');
        };
        $fields['class_date'] = function () {
            return \Yii::$app->formatter->asDate($this->class_date);
        };
        $fields['type'] = function () {
            switch ($this->type) {
                case 1:
                    return \Yii::t('zhtfrontend', '按学生');
                case 2:
                    return \Yii::t('zhtfrontend', '按课程');
            }
        };
        return $fields;
    }

    public function extraFields()
    {
        return [
            'mechanic_name'=> function(){
                return isset($this->mechanic) ? $this->mechanic->name : '';
            },
            'goods_name'=> function(){
                return isset($this->goods) ? $this->goods->name : '';
            },
            'status_name' => function () {
                switch ($this->status) {
                    case 0:
                        return \Yii::t('zhtfrontend', '未结算');
                    case 1:
                        return \Yii::t('zhtfrontend', '已结算');
                }
            }
//            'operate' => function () {
//                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
//            },
        ];
    }

    public function  getMechanic()
    {
        return $this->hasOne(Mechanic::className(), ['id' => 'mechanic_id']);
    }

    public function  getGoods()
    {
        return $this->hasOne(Goods::className(), ['id' => 'goods_id']);
    }


    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at']
                ],
            ],
        ]);
    }
}
